/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author anhvh_b00319
 */
@Entity
@Table(name = "Program")
@NamedQueries({
    @NamedQuery(name = "Program.findAll", query = "SELECT p FROM Program p"),
    @NamedQuery(name = "Program.findByProID", query = "SELECT p FROM Program p WHERE p.proID = :proID"),
    @NamedQuery(name = "Program.findByProName", query = "SELECT p FROM Program p WHERE p.proName = :proName"),
    @NamedQuery(name = "Program.findByProDescription", query = "SELECT p FROM Program p WHERE p.proDescription = :proDescription"),
    @NamedQuery(name = "Program.findByProPrice", query = "SELECT p FROM Program p WHERE p.proPrice = :proPrice")})
public class Program implements Serializable {

    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "Pro_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer proID;
    @Basic(optional = false)
    @Column(name = "Pro_Name")
    private String proName;
    @Basic(optional = false)
    @Column(name = "Pro_Description")
    private String proDescription;
    @Basic(optional = false)
    @Column(name = "Pro_Price")
    private double proPrice;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "program")
    private List<Enrollment> enrollmentList;
    @JoinColumn(name = "Teacher", referencedColumnName = "Sta_ID")
    @ManyToOne(optional = false)
    private Staff teacher;
    @Basic(optional = false)
    @Column(name = "StartDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate;
    @Basic(optional = false)
    @Column(name = "EndDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endDate;
    @Transient
    private String formatedStartDate;
    @Transient
    private String formatedEndDate;
    @Transient
    private List<Integer> clientList;

    public Program() {
        proPrice = 0;
        startDate = new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        endDate = c.getTime();
        enrollmentList = new ArrayList<Enrollment>();
    }

    public Program(Integer proID) {
        this.proID = proID;
    }

    public Program(Integer proID, String proName, String proDescription, double proPrice) {
        this.proID = proID;
        this.proName = proName;
        this.proDescription = proDescription;
        this.proPrice = proPrice;
    }

    public Integer getProID() {
        return proID;
    }

    public void setProID(Integer proID) {
        Integer oldProID = this.proID;
        this.proID = proID;
        changeSupport.firePropertyChange("proID", oldProID, proID);
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        String oldProName = this.proName;
        this.proName = proName;
        changeSupport.firePropertyChange("proName", oldProName, proName);
    }

    public String getProDescription() {
        return proDescription;
    }

    public void setProDescription(String proDescription) {
        String oldProDescription = this.proDescription;
        this.proDescription = proDescription;
        changeSupport.firePropertyChange("proDescription", oldProDescription, proDescription);
    }

    public double getProPrice() {
        return proPrice;
    }

    public void setProPrice(double proPrice) {
        double oldProPrice = this.proPrice;
        this.proPrice = proPrice;
        changeSupport.firePropertyChange("proPrice", oldProPrice, proPrice);
    }

    public List<Enrollment> getEnrollmentList() {
        return enrollmentList;
    }

    public void setEnrollmentList(List<Enrollment> enrollmentList) {
        this.enrollmentList = enrollmentList;
    }

    public Staff getTeacher() {
        return teacher;
    }

    public void setTeacher(Staff staff) {
        Staff oldStaff = this.teacher;
        this.teacher = staff;
        changeSupport.firePropertyChange("staff", oldStaff, staff);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (proID != null ? proID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Program)) {
            return false;
        }
        Program other = (Program) object;
        if ((this.proID == null && other.proID != null) || (this.proID != null && !this.proID.equals(other.proID))) {
            return false;
        }
        return true;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        Date oldStartDate = this.startDate;
        this.startDate = startDate;
        changeSupport.firePropertyChange("startDate", oldStartDate, startDate);
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        Date oldEndDate = this.endDate;
        this.endDate = endDate;
        changeSupport.firePropertyChange("endDate", oldEndDate, endDate);
    }

    public String getFormatedEndDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy");
        formatedEndDate = sdf.format(endDate);
        return formatedEndDate;
    }

    public void setFormatedEndDate(String formatedEndDate) {
        this.formatedEndDate = formatedEndDate;
    }

    public String getFormatedStartDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy");
        formatedStartDate = sdf.format(startDate);
        return formatedStartDate;
    }

    public void setFormatedStartDate(String formatedStartDate) {
        this.formatedStartDate = formatedStartDate;
    }

    public List<Integer> getClientList() {
        clientList = new ArrayList<Integer>();
        if (enrollmentList != null) {
            for (int i = 0; i < enrollmentList.size(); i++) {
                Enrollment enrollment = enrollmentList.get(i);
                clientList.add(enrollment.getClient().getClientID());
            }
        }
        if (clientList.isEmpty()) {
            clientList.add(-1);
        }
        return clientList;
    }

    public void setClientList(List<Integer> clientList) {
        this.clientList = clientList;
    }

    @Override
    public String toString() {
        return "Entities.Program[proID=" + proID + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
}
